
import type { HRBase, PaginationType } from "@/types"
import { reactive, ref } from "vue"

export default function usePageState() {
  // 响应式数据
  const loading = ref<boolean>(false)
  const submitLoading = ref<boolean>(false)
  const dialogVisible = ref<boolean>(false)
  const isEdit = ref<boolean>(false)
  // ELForm表单Ref引用;
  const formRef = ref()

  // 搜索表单
  const searchForm = reactive({
    name: '',
    email: '',
    status: ''
  })

  // 分页数据
  const pagination = reactive<PaginationType>({
    current: 1,
    size: 10,
    total: 0
  })

  // 表格数据
  const tableData = ref<HRBase[]>([
    {
      id: 1,
      name: '李小红',
      email: 'lixiaohong@company.com',
      avatar: '',
      role: 'Recruiter',
      permissions: ['job_management', 'candidate_management', 'interview_management'],
      jobCount: 8,
      status: 'active',
      createdAt: '2024-01-15T10:30:00',
      lastLoginAt: '2024-01-20T09:15:00'
    },
    {
      id: 2,
      name: '王小明',
      email: 'wangxiaoming@company.com',
      avatar: '',
      role: 'Recruiter',
      permissions: ['job_management', 'candidate_management'],
      jobCount: 5,
      status: 'active',
      createdAt: '2024-01-16T14:20:00',
      lastLoginAt: '2024-01-19T16:30:00'
    }
  ])

  // 表单数据
  const formData = reactive({
    id: null,
    name: '',
    email: '',
    password: '',
    confirmPassword: '',
    permissions: [],
    avatar: ''
  })

  // 表单验证规则
  const formRules = {
    name: [
      { required: true, message: '请输入HR姓名', trigger: 'blur' },
      { min: 2, max: 50, message: '姓名长度在2到50个字符', trigger: 'blur' }
    ],
    email: [
      { required: true, message: '请输入邮箱', trigger: 'blur' },
      { type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur' }
    ],
    password: [
      { required: true, message: '请输入密码', trigger: 'blur' },
      { min: 6, max: 20, message: '密码长度在6到20个字符', trigger: 'blur' }
    ],
    confirmPassword: [
      { required: true, message: '请确认密码', trigger: 'blur' },
      {
        // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-function-type
        validator: (rule: any, value: string, callback: Function) => {
          if (value !== formData.password) {
            callback(new Error('两次输入的密码不一致'))
          } else {
            callback()
          }
        },
        trigger: 'blur'
      }
    ],
    permissions: [
      { required: true, message: '请选择管理权限', trigger: 'change' }
    ]
  }
  return {
    loading,
    submitLoading,
    dialogVisible,
    isEdit,
    formRef,
    searchForm,
    pagination,
    tableData,
    formData,
    formRules
  }
}
